package net.sky.oa.dao.talkpark.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import net.sky.oa.dao.SkyBaseDaoImpl;
import net.sky.oa.dao.talkpark.TalCommentDao;
import net.sky.oa.model.TalComment;

@SuppressWarnings("unchecked")
@Repository
public class TalCommentDaoImpl extends SkyBaseDaoImpl<TalComment> implements
  		TalCommentDao {
	
	public TalCommentDaoImpl(){
		super.setPersistType(TalComment.class);
	}

	public List<TalComment> findAllTalComment() {
		
	   String hql="from TalComment tc";
	   
		return findByHql(hql);
	}
	
	public Map findTalCommentById(final int talblogId,final int currentPage,final int lineSize) throws Exception{
		Map map=new HashMap();
	    List list=getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer buffer=new StringBuffer("from TalComment tc where 1=1 ");
				StringBuffer cbuffer=new StringBuffer("select count(tc.id) from TalComment tc where 1=1");
				String hql="";
				if(talblogId!=-1){
					hql=" and tc.blog.id="+talblogId;
					buffer.append(hql);
					cbuffer.append(hql);
				}
				 buffer.append(" order by tc.createtime asc");
				 List resultList = new ArrayList();
				 Long count = (long)0;
				 Query query = session.createQuery(buffer.toString());
				 if(currentPage!=-1&&lineSize!=-1) {
			     count =(Long)session.createQuery(cbuffer.toString()).uniqueResult();
			     resultList = query.setFirstResult((currentPage - 1) * lineSize)
					.setMaxResults(lineSize).list();
				 }else {
						resultList = query.list();
					}
				    List list = new ArrayList();
					list.add(count);
					list.add(resultList);				
				    return list;
			}
	    });
	            map.put("count",list.get(0) );
	            map.put("list", list.get(1) );
		        return map;
	}

	

}
